// Copyright Rachael Colley 2013.

package rach.grid.andgrid1;

import java.util.Map;


public class MakePixelArray {

	private WorldParameters wp;
	private Map<Integer,Map<Integer,GridCell>> yMap;
	private int pixelList[];

	public MakePixelArray(WorldParameters worldParameters,Map<Integer,Map<Integer,GridCell>> yMap) {
		this.wp = worldParameters;
		this.yMap = yMap;
		buildPixelArray();
	}

	private int getTotalGridCellsForLevel() {
		int count = 0;
		for (int x = wp.getMinGridY(); x < wp.getMaxGridY() + 1; x ++) {
			Map<Integer,GridCell> current = yMap.get(x);
			count += current.size(); 
		}
		return count;
	}

	private int[] buildPixelArray() {
		pixelList = new int[getTotalGridCellsForLevel()];
		GridCellTypeEnum.GridCellCategory[] cellCategories = GridCellTypeEnum.GridCellCategory.values();
		int count=0;
		for (int x = wp.getMinGridY(); x < wp.getMaxGridY() + 1; x ++) {
			Map<Integer,GridCell> current = yMap.get(x);
			for (Map.Entry<Integer, GridCell> entry : current.entrySet()) {
				GridCell currentGC = entry.getValue();

				if (currentGC != null) {
					for (int j = 0; j < cellCategories.length; j++) {
						if (currentGC.getCellCategory().equals(cellCategories[j].toString())) {
							pixelList[count] = (Integer)GridCellTypeEnum.CategoryColors[j];
						}
					}
				}
				else {
					pixelList[count] = 0x00FFFFFF;
				}
				count ++;
			}
		}

		return pixelList;
	}
	
	public int[] getPixelList() {
		return pixelList;
	}

}
